package Presenter;


import java.util.List;

import javax.swing.DefaultListModel;

import AVLTree.AVLTree;
import Model.Player;
import Model.MarketPlace.MarketPlaceItem;
import Model.MarketPlace.MarketPlaceModel;
import Model.Planet.Planet;
import Model.Spaceship.Spaceship;
import View.MarketPlaceView;
/*
 * handles the market place logic for buying and selling
 */
public class MarketPlacePresenter{
	/**
	 * 
	 */
	private MarketPlaceModel model;

	/**
	 * 
	 */
	private Planet currentPlanet;

	/**
	 * Constructor 
	 */
	public MarketPlacePresenter(){
		this.model = new MarketPlaceModel(Player.getInstance().getCurrentPlanet());
		this.currentPlanet = Player.getInstance().getCurrentPlanet();
	}

	/**
	 * Creates an array and AVL Tree then puts the array of items in the AVL Tree.
	 * Then returns that list.
	 * @return a sorted list of items
	 */
	public List<MarketPlaceItem> getItemList(){
		MarketPlaceItem[] itemArray = currentPlanet.getItemList();
		AVLTree<MarketPlaceItem> list = new AVLTree<MarketPlaceItem>();
		for(int count = 0; count < itemArray.length; count++){
			list.add(itemArray[count]);
		}
		
		return list.asSortedList();
	}

	/**
	 * Getter for the marketplace view
	 * @return a new marketplace view
	 */
	public MarketPlaceView getView(){
		return new MarketPlaceView();
	}

	/**
	 * Checks if the current planet is changed
	 * @return a boolean
	 */
	public boolean isPlanetChanged(){
		//System.out.println(currentPlanet.getName() + "\t" + Player.getInstance().getCurrentPlanet().getName());
		if(currentPlanet.getName().equals(Player.getInstance()
		        .getCurrentPlanet().getName())){
			return false;
		}
		
		currentPlanet = Player.getInstance().getCurrentPlanet();
		return true;
	}

	/**
	 * A getter for the players cargo
	 * @return a linked list of the cargo items
	 */
	public List<MarketPlaceItem> getCargoList(){
		return Player.getInstance().getShip().getCargoItems();
	}

	/**
	 * A getter for the number of items in the cargo
	 * @return a number of item
	 */
	public int getCargoNumber(){
		return Player.getInstance().getShip().getCurrentCargo();
	}

	/**
	 * A getter for the credits a player has
	 * @return the credits of the player
	 */
	public double getCredits(){
		return Player.getInstance().getCredits();
	}

	/**
	 * Takes in an item and the quantity of the item  you want and returns a boolean
	 * if you can confirm the purchase
	 * @param item: what you want to buy
	 * @param quantity: the number of the items you want
	 * @return a boolean
	 */
	public boolean canBuy(MarketPlaceItem item, int quantity){
		if((Player.getInstance().getCredits() - (item.getPrice() * quantity)) >= 0 && 
		        Player.getInstance().getShip().getCurrentCargo() - quantity >= 0){
			return true;
		}
		
		return false;
	}

	/**
	 * Takes in an item and the quantity of the item  you want and returns a boolean
	 * if you can confirm the sale of that item
	 * @param item: what you want to sell
	 * @param quantity: the number you want to sell
	 * @return a boolean
	 */
	public boolean canSell(MarketPlaceItem item, int quantity){
		if(item.getQuantity() - quantity < 0){
			return false;
		}
		
		return true;
	}

	/**
	 * Actually does the handling of the purchasing in the items in the marketplace.
	 * It goes through a the cargo list and sees if there is space.
	 * If there is then the adds that item to players 
	 * It trusts that the item can be bought.
	 * cargo and takes away from the marketplace.
	 * @param item: what the player wants to buy
	 * @param quantity: how much of that item they want.
	 */
	public void buy(MarketPlaceItem item, int quantity){
		Spaceship ship = Player.getInstance().getShip();
		ship.setCurrentCargo(ship.getCurrentCargo() - quantity);
		item.setQuantity(item.getQuantity() - quantity);
		
		List<MarketPlaceItem> cargoList = Player.getInstance().getShip().getCargoItems();
		
		boolean found = false;
		for(int count = 0; count < cargoList.size(); count++){
			if(item.getName().equals(cargoList.get(count).getName())){
				cargoList.get(count).setQuantity(cargoList.get(count)
				        .getQuantity() + quantity);
				found = true;
				break;
			}
		}
		if(!found){
			if(quantity != 0){
				MarketPlaceItem mItem = new MarketPlaceItem(item.getName(), 
				        item.getPrice(), quantity, item.getDescription());
				cargoList.add(mItem);
			}

		}
		
		Player.getInstance().setCredits(Player.getInstance()
		        .getCredits() - (item.getPrice() * quantity));		
	}

	/**
	 * Actually does the handling of the selling of items in the marketplace.
	 * It goes through a the market list and sees 
	 * if the item the player wants is in there.
	 * If it is then the adds that item to the market list 
	 * and takes away from the cargo list.
	 * @param item
	 * @param marketList
	 * @param quantity
	 * @return
	 */
	public int sell(MarketPlaceItem item, DefaultListModel<MarketPlaceItem> marketList, int quantity){
		Spaceship ship = Player.getInstance().getShip();
		ship.setCurrentCargo(ship.getCurrentCargo() + quantity);
		item.setQuantity(item.getQuantity() - quantity);
		int quan = 0;
		
		for(int count = 0; count < marketList.size(); count++){
			if(marketList.get(count).getName().equals(item.getName())) {
				marketList.get(count).setQuantity(marketList.get(count).getQuantity()
				        + quantity);
				quan = marketList.get(count).getQuantity();
			}
		}
				
		Player.getInstance().setCredits(Player.getInstance()
		        .getCredits() + (item.getPrice() * quantity));
		return quan;
	}
	
	public String toString() {
		return super.toString();
	}

	/**
	 * Finds the index of a MarketPlaceItem item  inside a list of MarketPlaceItems by comparing their 
	 * name, description and price. Returns -1 if match not found within the list.
	 * @param item
	 * @param list
	 * @return index 
	 */
	public int findItemInList(MarketPlaceItem item, List<MarketPlaceItem> list){
		int index = 0;
		boolean found = false;
		
		for(int i = 0; i < list.size(); i++){
			MarketPlaceItem item2 = list.get(i);
			if((item.getName().equals(item2.getName())
					&& (item.getPrice() == item2.getPrice())
					&& (item.getDescription() == item2.getDescription()))){
				found = true;
				break;
			}
			else{
				index++;
			}
		}
		
		if(found){
			return index;
		}
		else{
			return -1;
		}
	}
}
